package cz.muni.fi.pa165.pokemon.dao.impl;

import cz.muni.fi.pa165.pokemon.dao.BadgeDAO;
import cz.muni.fi.pa165.pokemon.entity.Badge;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 * Class implements BadgeDAO interface functionality.
 *
 * @author Michal Toth
 */
@Repository
public class BadgeDAOImpl implements BadgeDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public void create(Badge badge) {
        if (badge == null) {
            throw new IllegalArgumentException("badge is null");
        }
        if (badge.getId() != null) {
            throw new IllegalArgumentException("badge.id is not null");
        }
        
        em.persist(badge);
    }

    @Override
    public Badge get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("null id");
        }
        Badge badge = em.find(Badge.class, id);
        return badge;
    }

    @Override
    public void update(Badge badge) {
        if (badge == null) {
            throw new IllegalArgumentException("badge is null");
        }
        if (badge.getId() == null) {
            throw new IllegalArgumentException("badge.id is null");
        }

        em.merge(badge);
    }

    @Override
    public void remove(Badge badge) {
        if (badge == null) {
            throw new IllegalArgumentException("badge is null");
        }
        if (badge.getId() == null) {
            throw new IllegalArgumentException("badge.id is null");
        }
        Badge c = em.find(Badge.class, badge.getId());
        em.remove(c);
    }

    @Override
    public List<Badge> findAll() {
        Query query = em.createQuery("SELECT c FROM Badge c");
        List<Badge> results = query.getResultList();

        return results;
    }

    @Override
    public List<Badge> findByName(String firstName, String lastName) {
        boolean firstNameNull = (firstName == null);
        boolean lastNameNull = (lastName == null);
        if (lastNameNull && firstNameNull) {
            throw new IllegalArgumentException("null lastName and firstName");
        }

        Query query;
        if (firstNameNull) {
            // firstName is null, query for lastName only
            query = em.createQuery("SELECT c FROM Badge c WHERE c.lastName like :lastName");
            query.setParameter("lastName", lastName);
        } else if (lastNameNull) {
            query = em.createQuery("SELECT c FROM Badge c WHERE c.firstName like :firstName");
            query.setParameter("firstName", firstName);
        } else {
            query = em.createQuery("SELECT c FROM Badge c WHERE c.firstName like :firstName AND c.lastName like :lastName");
            query.setParameter("lastName", lastName);
            query.setParameter("firstName", firstName);
        }

        List<Badge> results = query.getResultList();
        return results;
    }
}